<template>

  <ul class="todo-main">
    <Item v-for="(todo, index) in todos" :key="todo.id" 
      :todo="todo" :index="index" />
  </ul>
</template>

<script>
import {mapState} from 'vuex'
import Item from './Item.vue'
export default {
  name: 'List',

  computed: {
    ...mapState(['todos'])
    // todos () {
    //   return this.$store.state.todos
    // }
  },

  watch: {
    todos: {
      handler (value) {
        console.log('handler', value)
        // 将todos保存localStorage中 (必须保存json格式)
        localStorage.setItem('todos_key', JSON.stringify(value))
      },
      deep: true, // 深度监视 ==> 数组内部的对象内部数据变化都会触发
    }
  },

  components: {
    Item
  }
}
</script>

<style scoped>
  .todo-main {
    margin-left: 0px;
    border: 1px solid #ddd;
    border-radius: 2px;
    padding: 0px;
  }

  .todo-empty {
    height: 40px;
    line-height: 40px;
    border: 1px solid #ddd;
    border-radius: 2px;
    padding-left: 5px;
    margin-top: 10px;
  }
</style>
